﻿using System;
using System.Drawing;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Helpers.Visualizer;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Screens
{
	/// <summary>
	/// Manages the UI to enter Random Seed Number to start.
	/// </summary>
	class SeedScreen : Screen
	{

		Panel MainPanel;

        #region Constructors

        public SeedScreen()
		{
			PanelList.Add(new Panel(new Point(4, 3),
				Visualizer.GetScreenWidth() - 9, Visualizer.GetScreenHeight() - 6));
			MainPanel = PanelList[0];
		}

        #endregion

        #region Methods

        public override void Show()
		{
			Clear();

			this.MainPanel.Clear();
			this.MainPanel.Write(Properties.Strings.RequestSeed, 0, ConsoleColor.White, TextAlignment.Multi);
			MainPanel.Draw();

			//we needs some robust error checking prevention here. grab first 5 characters then try catch?
			//we could also create a readline in the input manager or even a read number.
            GetSeedFromUser();

			this.MainPanel.Clear();

			this.MainPanel.Write(Properties.Strings.LoadingSeed + Convert.ToString(ApplicationManager.Instance.Profile.Session.TempSeed), 0, ConsoleColor.White, TextAlignment.Single);
			MainPanel.Draw();

			WaitKey();

			ApplicationManager.Instance.SoundManager.PlaySound(SoundList.Pluck.ToString());

			ApplicationManager.Instance.ScreenStack.Push(new GameScreen());


		}

		private static void GetSeedFromUser()
		{
			int seed = 0;
			//HACK: hard coded a quick fix to get the seed input in the right place
			//Better to have a formal way to aks questions
			Console.SetCursorPosition(35,5);
			string input = Console.ReadLine();
			if (input.Length > 5) input = input.Remove(5);
			try
			{
				seed = Convert.ToInt32(input);
                SeedHelper.SetSeed(seed);
			}
			catch
			{
				GameManager.Instance.SoundManager.PlaySound(SoundList.Pluck.ToString());
                GetSeedFromUser();
			}
		}
		#endregion
	}
}
